﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MM.Commmon.UI;
using MM.Common.DBBase;
using System.Xml;
using MM.Common.Base;
using Microsoft.Reporting.WinForms;
using System.IO;

namespace MM.Common.Setup
{
    public partial class FrmViewRDLC : FrmFunctionWithTool
    {
        public FrmViewRDLC()
        {
            InitializeComponent();
        }

        private void FrmViewRDLC_Load(object sender, EventArgs e)
        {
            this.rptViewer1.RefreshReport();
        }

        protected bool AddDataSource(DataParamCol parm)
        {
            bool flag = true;
            XmlDocument document = new XmlDocument();
            document.Load(this.rptViewer1.LocalReport.ReportPath);
            XmlElement documentElement = document.DocumentElement;
            this.rptViewer1.LocalReport.DataSources.Clear();
            int count = document.GetElementsByTagName("DataSet").Count;
            for (int i = 0; i < count; i++)
            {
                string name = document.GetElementsByTagName("DataSet").Item(i).Attributes["Name"].Value;
                string str2 = document.GetElementsByTagName("CommandText").Item(i).InnerText.Trim();
                bool flag2 = str2.Substring(0, 7).ToUpper() != "SELECT ";
                DataTable dt = new DataTable();
                if (flag2)
                {
                    flag = SystemInfo.DbData.RetrieveProcedure(str2, parm, ref dt);
                }
                else
                {
                    flag = SystemInfo.DbData.Retrieve(str2, parm, ref dt);
                }
                if (flag)
                {
                    this.rptViewer1.LocalReport.DataSources.Add(new ReportDataSource(name, dt));
                }
                else
                {
                    MessageBox.Show("获取数据不成功！\r\n" + SystemInfo.DbData.ErrorMessage);
                    break;
                }
            }
            count = document.GetElementsByTagName("CodeModules").Count;
            if (count > 0)
            {
                this.rptViewer1.LocalReport.ExecuteReportInCurrentAppDomain(AppDomain.CurrentDomain.Evidence);
                for (int j = 0; j < count; j++)
                {
                    string assemblyName = document.GetElementsByTagName("CodeModule").Item(j).InnerText.Trim();
                    this.rptViewer1.LocalReport.AddTrustedCodeModuleInCurrentAppDomain(assemblyName);
                }
            }
            return flag;
        }

        protected void DisplayReport(ReportParameter[] rparm)
        {
            if (rparm != null)
            {
                this.rptViewer1.LocalReport.SetParameters(rparm);
            }
            this.rptViewer1.RefreshReport();
        }
        protected bool InitReport(string rptname)
        {
            if (File.Exists(rptname))
            {
                this.rptViewer1.LocalReport.ReportPath = rptname;
                this.rptViewer1.LocalReport.DataSources.Clear();
                return true;
            }
            return false;
        }

        private void rptViewer1_ReportError(object sender, ReportErrorEventArgs e)
        {
            MessageBox.Show(e.Exception.Message);
        }
    }
}
